/**
 * 提现相关工具函数
 */

// 提现状态映射
export const WITHDRAW_STATUS = {
    PENDING: 'PENDING',     // 待审批
    APPROVED: 'APPROVED',   // 已通过
    REJECTED: 'REJECTED',   // 已拒绝
    COMPLETED: 'COMPLETED'  // 已完成
};

// 获取状态文本
export function getStatusText(status) {
    const statusMap = {
        [WITHDRAW_STATUS.PENDING]: '待审批',
        [WITHDRAW_STATUS.APPROVED]: '已通过',
        [WITHDRAW_STATUS.REJECTED]: '已拒绝',
        [WITHDRAW_STATUS.COMPLETED]: '已完成'
    };
    return statusMap[status] || '未知状态';
}

// 获取状态颜色
export function getStatusColor(status) {
    const colorMap = {
        [WITHDRAW_STATUS.PENDING]: '#f39c12',
        [WITHDRAW_STATUS.APPROVED]: '#27ae60',
        [WITHDRAW_STATUS.REJECTED]: '#e74c3c',
        [WITHDRAW_STATUS.COMPLETED]: '#3498db'
    };
    return colorMap[status] || '#95a5a6';
}

// 验证提现金额
export function validateWithdrawAmount(amount, availableAmount) {
    const numAmount = parseFloat(amount);
    
    if (isNaN(numAmount) || numAmount <= 0) {
        return { valid: false, message: '请输入有效的提现金额' };
    }
    
    if (numAmount < 10) {
        return { valid: false, message: '最低提现金额为10元' };
    }
    
    if (numAmount > availableAmount) {
        return { valid: false, message: '提现金额不能超过可提现金额' };
    }
    
    return { valid: true, message: '' };
}

// 格式化金额显示
export function formatAmount(amount) {
    if (typeof amount === 'string') {
        amount = parseFloat(amount);
    }
    return amount.toFixed(2);
}

// 格式化时间显示
export function formatTime(timeStr) {
    if (!timeStr) return '';
    
    const date = new Date(timeStr);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    
    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

// 检查是否支持微信商家转账
export function checkMerchantTransferSupport() {
    return uni.canIUse('requestMerchantTransfer');
}

// 处理微信商家转账
export function handleMerchantTransfer(params) {
    return new Promise((resolve, reject) => {
        if (!checkMerchantTransferSupport()) {
            uni.showModal({
                content: '您的微信版本过低，请更新至最新版本。',
                showCancel: false,
            });
            reject(new Error('微信版本不支持'));
            return;
        }
        
        uni.requestMerchantTransfer({
            mchId: params.mchId,
            appId: params.appId,
            package: params.package,
            success: (res) => {
                console.log('商家转账成功:', res);
                resolve(res);
            },
            fail: (res) => {
                console.log('商家转账失败:', res);
                reject(res);
            },
        });
    });
}

// 计算手续费（示例计算方式，实际应该由后端计算）
export function calculateFee(amount, feeRate = 0.006) {
    const numAmount = parseFloat(amount);
    if (isNaN(numAmount) || numAmount <= 0) return 0;
    
    const fee = numAmount * feeRate;
    const minFee = 2; // 最低手续费2元
    const maxFee = 25; // 最高手续费25元
    
    return Math.max(minFee, Math.min(fee, maxFee));
}

// 计算实际到账金额
export function calculateRealAmount(amount, feeAmount) {
    const numAmount = parseFloat(amount);
    const numFee = parseFloat(feeAmount);
    
    if (isNaN(numAmount) || isNaN(numFee)) return 0;
    
    return Math.max(0, numAmount - numFee);
}